﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.UI.Panel;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    /// <summary>
    /// 学习经历的Item
    /// </summary>
    public class StudyExpItem:UIBase<StudyExpItem>
    {
        public InputField BeginEndTime;//开始结束的时间
        public InputField SchoolNameText;//学校的名字
        public InputField JobText;//职位
        public InputField CertifierText;//证明人
        public Button DeleteButton;
        protected override UIType UIType
        {
            get { return UIType.Item;}
        }
        protected override void InitOnAwake()
        {
            AutoFindComponent(transform, true);
        }

        public void Init(StudyExpInfo info)
        {
            JobText.text = info.jobstatus;
            BeginEndTime.text = info.starttime;
            SchoolNameText.text = info.schoolname;
            CertifierText.text = info.certifier;
            BeginEndTime.onEndEdit.AddListener(x =>
            {
                //日期格式：1992/06-2012/12  
                if (string.IsNullOrEmpty(x)) return;
                info.starttime = Regex.IsMatch(x, @"^(\d{4}/((0\d)|(1[0-2])))-(\d{4}/((0\d)|(1[0-2])))$") ? x : null;
                if (info.starttime == null)
                {
                    BeginEndTime.text = null;
                    UIManager._Instance.ShowTipInfo("请输入正确的日期格式");
                }
                else//输入符合格式
                {
                    string[] times = info.starttime.Split('-');
                    DateTimeFormatInfo dtFormat = new DateTimeFormatInfo {ShortDatePattern = "yyyy/MM/dd"};
                    var dt01 = Convert.ToDateTime(times[0]+"/01", dtFormat);
                    var dt02 = Convert.ToDateTime(times[1] + "/01", dtFormat);
                    //Debug.Log("dt02.CompareTo(dt01):" + dt02.CompareTo(dt01));
                    if (dt02.CompareTo(dt01) == -1)//说明dt02在dt01之前不符合规则
                    {
                        info.starttime = null;
                        BeginEndTime.text = null;
                        UIManager._Instance.ShowTipInfo("输入日期有误");
                    }
                }
            });
            SchoolNameText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x)) return;
                info.schoolname= Regex.IsMatch(x, @"^[\u4e00-\u9fa5]+$") ? x : null;
                if (info.schoolname == null)
                {
                    SchoolNameText.text = null;
                    UIManager._Instance.ShowTipInfo("学名限定中文");
                }
            });
            JobText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x)) return;
                info.jobstatus = Regex.IsMatch(x, @"^[\u4e00-\u9fa5]+$") ? x : null;
                if (info.jobstatus == null)
                {
                    JobText.text = null;
                    UIManager._Instance.ShowTipInfo("职称限定中文");
                }
            });
            CertifierText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x)) return;
                info.certifier = Regex.IsMatch(x, @"^[\u4e00-\u9fa5]+$") ? x : null;
                if (info.certifier == null)
                {
                    CertifierText.text = null;
                    UIManager._Instance.ShowTipInfo("姓名限定中文");
                }
            });
            DeleteButton.onClick.AddListener(() =>
            {
                StudentInfoPanel._Instance.RemoveStudyExpInfo(info);
                Destroy(gameObject);
            });
        }
    }
}